//
//  LearningChooseBrithdayView.swift
//  LearningLanguage
//
//  Created by Rae on 2023/9/7.
//

import UIKit
import SwiftEntryKit

class VRChooseBrithdayView: UIView, SAAnimationDelegate, SAAnimationProtocol{

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 300.0)
    }
    
    func animationContainerViewStyle() -> SAAnimationViewStyle {
        .bottom
    }
    
//    func animationContainerScreenInteraction() -> EKAttributes.UserInteraction {
//        .dismiss
//    }
//
//    func animation(customAttributes attributes: EKAttributes) -> EKAttributes {
//        var attri = attributes
//        attri.positionConstraints.verticalOffset = 0
//        return attri
//    }
    
    private var datePicker          = UIDatePicker()
    private let sureButton          = c_make_ex(UIButton.self)
        .color("FFFC00").text("Done").font(18.font_bold).end()
    private let cancelButton        = c_make_ex(UIButton.self)
        .color(.white).text("Cancel").font(18.font_bold).end()
    var dateStr = ""
    var didChooseCompletion: ((_ dateStr: String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#1F1E18".toColor
        self.fj.corner(20, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        subviews{
            sureButton
            cancelButton
            datePicker
        }
        
        sureButton.trailing(15).top(15)
        cancelButton.leading(15).top(15)
        datePicker.leading(15).width(.screenWidth-30).height(200).top(55)
        
        datePicker.locale = Locale(identifier: "en_US")
        datePicker.datePickerMode = .date
        datePicker.date = Date(timeIntervalSinceNow: -(60*60*24*365*18))
        datePicker.maximumDate = Date(timeIntervalSinceNow: -(60*60*24*365*18))
        datePicker.tintColor = UIColor.white
        datePicker.setValue(UIColor.white, forKeyPath: "textColor")
        dateStr = Date(timeIntervalSinceNow: -(60*60*24*365*18)).toString()
        if #available(iOS 13.4, *) {
            datePicker.preferredDatePickerStyle = .wheels
        }
        //注意：action里面的方法名后面需要加个冒号“：”
        datePicker.addTarget(self, action: #selector(dateChanged),
                                 for: .valueChanged)
        
        sureButton.addTarget(self, action: #selector(clickDoneButton), for: .touchUpInside)
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //日期选择器响应方法
    @objc func dateChanged(){
        //更新提醒时间文本框
        dateStr = datePicker.date.toString()
    }
    @objc private func clickDoneButton(){
        
        if !self.anySubViewScrolling(view: datePicker) {
            guard let closure = self.didChooseCompletion else { return }
            saAnimate.animation(dismiss: self)
            closure(dateStr)
        }
    }
    @objc private func clickCancelButton(){
        saAnimate.animation(dismiss: self)
    }
    
    func anySubViewScrolling(view: UIView) -> Bool {
        
        if view.isKind(of: UIScrollView.self) {
            let scrV = view as! UIScrollView
            if scrV.isDragging || scrV.isDecelerating {
                return true
            }
        }
        for subV in view.subviews {
            if self.anySubViewScrolling(view: subV){
                return true
            }
        }
        return false
    }
}
